package com.clqteam.controller;

import java.io.IOException;
import java.io.PrintWriter;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.clqteam.model.Employee;
import com.clqteam.service.EmployeeLoginService;
import com.clqteam.serviceimpl.EmployeeLoginServiceImpl;
/**
 * 
 * @author CLQ
 *
 */
public class EmployeeLoginController extends HttpServlet{
	 EmployeeLoginService emService=new EmployeeLoginServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    	   doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    	req.setCharacterEncoding("utf8");
    	resp.setCharacterEncoding("utf8");
    	String name=req.getParameter("inputName");
  	    String psw= req.getParameter("inputPsw");
  	    String check=req.getParameter("check");
   	    Employee em= emService.findEmployeeAccountAndEmployeePsw(name, psw);
  	    HttpSession session=req.getSession();
  	    PrintWriter pw=resp.getWriter();
  	     if(em.geteId()>0){
  	    	 if("on".equals(check)){
  	    	 Cookie cookie=new Cookie("name", name);
  	    	 Cookie cookie1=new Cookie("psw", psw);
  	    	 cookie.setMaxAge(60*60*24);
  	    	 cookie1.setMaxAge(60*60*24);
  	    	 resp.addCookie(cookie);
  	    	 resp.addCookie(cookie1);
  	    	 }else{}
  	    	   //将用户信息放入session 
  	    	session.setAttribute("employee", em);
  	    	//向数据库中添加日志
  	    	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  	    	Date  date=new Date();
  	    	String str=sdf.format(date);  
  	    	String s="用户登陆成功！";
  	    	emService.insertLoginTime(str, s, em.geteId(),em.geteName());
  	    	
  	    	 pw.write("1");
  	     }else{
  	    	 pw.write("0");
  	     }
  	    pw.flush();
  	    pw.close();

 
    	
    }
}
